import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const Login = () => import('../views/login/index.vue');
const Register = () => import('../views/register/register.vue')
const PasswordReset = () => import('../views/login/passwordReset.vue')
const Main = () => import('../views/Main.vue')
const DataDictionary = () => import('../views/basedata/dataDictionary.vue')
const LoginLog = () => import('../views/basedata/loginLog.vue')
const HouseList = () => import('../views/house/houseList.vue')
const AddHouse = () => import('../views/house/addHouse.vue')
const Estate = () => import('../views/house/estate.vue')
const HouseRequest = () => import('../views/house/houseRequest.vue')
const PromisePage = () => import('../views/house/promisePage.vue')
const RentHouse = () => import('../views/house/rentHouse.vue')
const HouseHost = () => import('../views/house/houseHost.vue')
const Message = () => import('../views/house/message.vue')
const UserList = () => import('../views/user/userList.vue')
const RoleList = () => import('../views/user/roleList.vue')
const PermissionList = () => import('../views/user/permissionList.vue')
const Home = () => import('../views/index/home.vue')
const Question = () => import('../views/myQuestion/question.vue')
const Advertist = () => import('../views/house/advertist.vue')
const TaskList = () => import('../views/task/taskList.vue')
const InterfaceList = () => import('../views/interfaceRequest/interfaceList.vue')

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/main',
    redirect: '/home'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/password-reset',
    component: PasswordReset
  },
  {
    path: '/main',
    component: Main,
    children: [
      {
        path: '/home',
        component: Home

      },
      {
        path: '/data',
        component: DataDictionary
      },
      {
        path: '/login-log',
        component: LoginLog
        
      },
      {
        path: '/house-list',
        component: HouseList
      },
      {
        path: '/add-house',
        component: AddHouse
      },
      {
        path: '/estate',
        component: Estate
      },
      {
        path: '/house-request',
        component: HouseRequest
      },
      {
        path: '/promise-page',
        component: PromisePage
      },
      {
        path: '/rent-house',
        component: RentHouse
      },
      {
        path: '/house-host',
        component: HouseHost
      },
      {
        path: '/message',
        component: Message
      },
      {
        path: '/userList',
        component: UserList
      },
      {
        path: '/roleList',
        component: RoleList
      },
      {
        path: '/permission-list',
        component: PermissionList
      },
      {
        path: '/question',
        component: Question
      },
      {
        path: '/advertist',
        component: Advertist
      },
      {
        path: '/task-list',
        component: TaskList
      },
      {
        path: '/interface-list',
        component: InterfaceList
      }
    ]
  }
  
]

const router = new VueRouter({
  routes
});

//路由拦截
router.beforeEach((to,from,next) => {
   //to将要访问的路径
  //from 代表那个路径跳转而来
  //next 是一个函数，表示放行  next('/login') 强制跳转
  //如果当前用户处于注册页,直接放行
  if (to.path === '/register'){
    return next();
  }
  //如果当前用户处于更新密码页,直接放行
  if (to.path === '/password-reset'){
    return next();
  }
  //如果当前用户处于登录页,直接放行
  if (to.path === '/login'){
    return next();
  }
  //获取后端token
  const tokenStr = window.sessionStorage.getItem("token")
  //如果token失效，返回登录页
  if (!tokenStr){
    return next('/login');
  }
  return next();
});

export default router;
